import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:xsdk_flutter/xsdk/framework/bean/x_bean_data_table.dart';
import 'package:xsdk_flutter/xsdk/framework/page/x_page_state.dart';
import 'package:xsdk_flutter/xsdk/operation/x_constant_temp.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_color.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';

/*
注意:
多个滚动控件联动时,需要过滤单向操作,避免控件之间的位置变化循环触发造成卡顿,
解决:用Listener触摸监听判断当前是哪个控件

待优化: SingleChildScrollView的虚拟滚动,只加载可视区域
 */
abstract class XPageTableState extends XPageState {
  //冻结左列数
  int frozenLeftColumnsCount = 0;
  //冻结右列数
  int frozenRightColumnsCount = 0;
  double tableTitleLabelRowHeight = 48;
  Color? tableTitleLabelBottomDividerColor;
  Color? tableTitleLabelRowBackgroundColor;
  Color? tableFrozenEndLinesBackgroundColor;
  Color tableBackgroundColor;
  Color? frozenDividerColor;
  Color tableBorderColor;
  bool isShowTableBorder=false;

  XPageTableState({
    super.isKeepPageState = false,
    super.xTag,
    super.handleAndFinishPageFunction,
    this.frozenLeftColumnsCount = 0,
    this.frozenRightColumnsCount = 0,
    this.tableTitleLabelRowHeight = 48,
    this.tableTitleLabelRowBackgroundColor=const Color(0xFFEEEEEE),
    this.tableTitleLabelBottomDividerColor=const Color(0xFFEEEEEE),
    this.tableFrozenEndLinesBackgroundColor,
    this.frozenDividerColor=const Color(0xFFCCCCCC),
    this.tableBackgroundColor=Colors.white,
    this.tableBorderColor=const Color(0xFFEEEEEE),//每行分割线,表格线(默认显示每行分隔线)
    this.isShowTableBorder=false,//表格线
  });

  //横向滑动
  final ScrollController _scrollController_h_tableTitleLabel = ScrollController();
  final ScrollController _scrollController_h_tableContext = ScrollController();
  final ScrollController _scrollController_h_tableEndLine = ScrollController();

  //竖向滑动
  late ScrollController xScrollController;
  late NotificationListenerCallback<Notification> onScrollNotification;

  /*
  1表头标签横向滚动
  2表内容横向滚动
  3表最后一行数据
   */
  int _currentScrollControllerIndex = 0;

  //判断所有列的固定宽度是否超过屏幕宽度(竖屏)
  bool _isOverScreenWidth = false;

  @override
  void onCreate() {
    widget.dataTableColumnsLabels.clear();
    widget.dataTableColumnsLabels.addAll(getTableColumnLabels());
    double allColumnWidth = 0;
    for (XBeanDataTableLabel citem in widget.dataTableColumnsLabels) {
      if (citem.columnWidth != null) {
        allColumnWidth += citem.columnWidth!;
      }
    }
    if (allColumnWidth > XTempData.screenWidth) {
      _isOverScreenWidth = true;
    }

    xScrollController = ScrollController(initialScrollOffset:widget.currentListViewPosition);

    onScrollNotification= (Notification notification){

      if (notification is ScrollStartNotification) {
        // 滚动开始时重置状态
        _isScrolling = true;
        _cancelDebounceTimer();
      }else if (notification is ScrollUpdateNotification) {
        // 滚动更新时保持滚动状态
        _isScrolling = true;
        _cancelDebounceTimer();
      }else if(notification is ScrollEndNotification){
        //监听停止滚动

        //记录当前位置
        widget.currentListViewPosition=xScrollController.offset??0.0;

        //滚动底部停止(适用item占满这个listview情况,否则滚不到无法激活监听)
        if(notification.metrics.extentAfter == 0){
          //已经滑到底部,加载下一页
          if(getIsEnablePageLoadData()){
            if(widget.currentPageIndex<widget.totalPages){
              //启动防抖计时
              _isScrolling = false;
              _startDebounceTimer();
            }
          }
        }

        return true;
      }
      return false;
    };

  }

  @override
  void onDestroy() {
    _scrollController_h_tableTitleLabel.dispose();
    _scrollController_h_tableContext.dispose();
    _scrollController_h_tableEndLine.dispose();
    _cancelDebounceTimer();//防抖计时
    xScrollController.dispose();
  }

  //
  Widget? getTableHeaderWidget() {
    return null;
  }

  Widget? getTableFooterWidget() {
    return null;
  }

  //背景圆角Widget(在一个圆角Widget中)
  bool getIsUseRoundedRectWidgetBackground(){
    return false;
  }

  //是否启用分页加载数据
  bool getIsEnablePageLoadData(){
    return false;
  }

  //是否启用下拉刷新数据
  bool getIsEnableRefreshLoadData(){
    return false;
  }

  @override
  bool getIsPageIndexStartWith1() {
    //分页加载是否从第1页开始,否则从0开始
    return true;
  }

  @required
  List<XBeanDataTableLabel> getTableColumnLabels();

  /*
  每一行数据UI
  TableRow添加分割线
  decoration:XWidgetUtils.getBoxDecoration(
              backgroundColor:tableTitleLabelRowBackgroundColor,
              border: Border(bottom: BorderSide(color: XColorUtils.dividerColor,width: 0.5)
              )
          );
   */
  @required
  List<Widget> getTableItemRowWidgets(dynamic rowItemList);

  @required
  List<Widget>? getTableEndFrozenRowWidgets(dynamic rowItemList);



  @override
  Widget getBodyWidget(BuildContext context) {
    List<Widget> allWidgets = [];
    //头部widget
    Widget? tableHeaderWidget = getTableHeaderWidget();
    if (tableHeaderWidget != null) {
      allWidgets.add(tableHeaderWidget);
    }

    if (!_isOverScreenWidth ||
        (frozenLeftColumnsCount == 0 && frozenRightColumnsCount == 0)) {
      //所有列不超过屏幕宽度,没有冻结列数
      _handleNoHasFrozenColumns(allWidgets);
    } else {
      //有冻结
      _handleHasFrozenColumns(allWidgets);
    }

    //底部widget
    Widget? tableFooterWidget = getTableFooterWidget();
    if (tableFooterWidget != null) {
      allWidgets.add(tableFooterWidget);
    }

    if(getIsUseRoundedRectWidgetBackground()){
      return isShowTableBorder?XWidgetUtils.getXLayoutRoundedRectWithBorder(
          borderColor: tableBorderColor,
          backgroundColor: tableBackgroundColor,
          margin: XWidgetUtils.getEdgeInsets(10, 10, 10, 10),
          child: XWidgetUtils.getLayoutColumn(
            children: allWidgets,
          )):XWidgetUtils.getXLayoutRoundedRect(
          backgroundColor: tableBackgroundColor,
          margin: XWidgetUtils.getEdgeInsets(10, 10, 10, 10),
          child: XWidgetUtils.getLayoutColumn(
            children: allWidgets,
          ));
    }else{
      //
      return XWidgetUtils.getLayoutContainer(
          decoration: isShowTableBorder?XWidgetUtils.getBoxDecoration(
            backgroundColor: tableBackgroundColor,
            border: Border.all(color:tableBorderColor, style: BorderStyle.solid, width:0.5),
          ):null,
          backgroundColor: tableBackgroundColor,
          child: XWidgetUtils.getLayoutColumn(
            children: allWidgets,
          ));
    }

  }


  //处理没有冻结列的情况
  void _handleNoHasFrozenColumns(List<Widget> allWidgets) {
    if (widget.dataTableColumnsLabels.isEmpty) {
      return;
    }

    //每列宽度
    Map<int, TableColumnWidth> columnWidths =
    _getColumnWidths(widget.dataTableColumnsLabels);

    //表头标签
    Widget tableHeaderLabelWidget = Table(
      //border: tableBorder,
      columnWidths: columnWidths,
      children: [_getTableRow_tableTitleLabel(widget.dataTableColumnsLabels)],
    );

    if (_isOverScreenWidth) {
      allWidgets.add(
          XWidgetUtils.getLayoutContainer(
            backgroundColor: tableTitleLabelRowBackgroundColor,
            decoration: tableTitleLabelBottomDividerColor!=null?
            XWidgetUtils.getBoxDecoration(
                backgroundColor:tableTitleLabelRowBackgroundColor,
                border: Border(bottom: BorderSide(color: tableTitleLabelBottomDividerColor!,width: 0.5)
                )
            ):null,
            child: XWidgetUtils.getWidgetListener(
                child: XWidgetUtils.getLayoutSingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    //initialScrollOffset: initialHorizontalScrollOffset,
                    scrollController: _scrollController_h_tableTitleLabel,
                    onNotifyScrollChangePosition: (position) {
                      if (_currentScrollControllerIndex == 1) {
                        if (widget.listData.isNotEmpty) {
                          _scrollController_h_tableContext.jumpTo(position);
                        }
                        if (widget.listData2.isNotEmpty) {
                          _scrollController_h_tableEndLine.jumpTo(position);
                        }
                      }
                    },
                    child: tableHeaderLabelWidget),
                onPointerDown: (PointerDownEvent event) {
                  _currentScrollControllerIndex = 1;
                }
            ),
          )
      );
    } else {
      allWidgets.add(
          XWidgetUtils.getLayoutContainer(
              backgroundColor: tableTitleLabelRowBackgroundColor,
              decoration: tableTitleLabelBottomDividerColor!=null?
              XWidgetUtils.getBoxDecoration(
                  backgroundColor:tableTitleLabelRowBackgroundColor,
                  border: Border(bottom: BorderSide(color: tableTitleLabelBottomDividerColor!,width: 0.5)
                  )
              ):null,
              child:tableHeaderLabelWidget
          ));
    }

    //表内容
    if (widget.listData.length != 0) {
      Widget tableContextWidget = Table(
        //border: tableBorder,
        columnWidths: columnWidths,
        children: widget.listData.map((dynamic rowItemList) {

          List<Widget> srcL=getTableItemRowWidgets(rowItemList);
          List<Widget>? newL;

          if(isShowTableBorder){
            newL=[];
            int size=srcL.length;
            for(int i=0;i<size;i++){
              if(i!=size-1){
                newL.add(XWidgetUtils.getLayoutContainer(
                    decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                    child: srcL[i]
                ));
              }else{
                newL.add(srcL[i]);
              }
            }
          }

          return TableRow(
            children: newL??srcL,
            decoration: XWidgetUtils.getBoxDecoration(
                border: Border(
                    bottom: BorderSide(color: tableBorderColor, width: 0.5))),
          );

        }).toList(),
      );

      allWidgets.add(XWidgetUtils.getLayoutExpanded(
          XWidgetUtils.getLayoutSingleChildScrollView(
            //支持分页
              initialScrollOffset: widget.currentListViewPosition,
              scrollController: xScrollController,
              onScrollNotification: onScrollNotification,
              onRefresh: !getIsEnableRefreshLoadData()?null:() async{
                widget.currentListViewPosition=0.0;
                updateData(pageIndex: getIsPageIndexStartWith1()?1:0);
              },
              child: _isOverScreenWidth
                  ? XWidgetUtils.getWidgetListener(
                  child: XWidgetUtils.getLayoutSingleChildScrollView(
                      scrollDirection: Axis.horizontal,
                      scrollController: _scrollController_h_tableContext,
                      //initialScrollOffset: initialHorizontalScrollOffset,
                      onNotifyScrollChangePosition: (position) {
                        if (_currentScrollControllerIndex == 2) {
                          if (widget.dataTableColumnsLabels.isNotEmpty) {
                            _scrollController_h_tableTitleLabel
                                .jumpTo(position);
                          }
                          if (widget.listData2.isNotEmpty) {
                            _scrollController_h_tableEndLine
                                .jumpTo(position);
                          }
                        }
                      },
                      child: tableContextWidget),
                  onPointerDown: (PointerDownEvent event) {
                    _currentScrollControllerIndex = 2;
                  })
                  : tableContextWidget)));
    }

    //冻结最后n行数据(统计)
    if (widget.listData2.length != 0) {
      //
      Widget tableFrozenEndLineWidget = Table(
        //border: tableBorder,
        columnWidths: columnWidths,
        children: widget.listData2.map((dynamic rowItemList) {


          List<Widget> srcL=getTableEndFrozenRowWidgets(rowItemList)!;
          List<Widget>? newL;

          if(isShowTableBorder){
            newL=[];
            int size=srcL.length;
            for(int i=0;i<size;i++){
              if(i!=size-1){
                newL.add(XWidgetUtils.getLayoutContainer(
                    decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                    child: srcL[i]
                ));
              }else{
                newL.add(srcL[i]);
              }
            }
          }


          return TableRow(children: newL??srcL);
        }).toList(),
      );

      if (_isOverScreenWidth) {
        allWidgets.add(XWidgetUtils.getLayoutContainer(
            backgroundColor: tableFrozenEndLinesBackgroundColor,
            decoration: frozenDividerColor!=null?
            XWidgetUtils.getBoxDecoration(
                backgroundColor:tableFrozenEndLinesBackgroundColor,
                border: Border(top: BorderSide(color: frozenDividerColor!,width: 0.5)
                )
            ):null,
            child:XWidgetUtils.getWidgetListener(
            child: XWidgetUtils.getLayoutSingleChildScrollView(
                scrollDirection: Axis.horizontal,
                //initialScrollOffset: initialHorizontalScrollOffset,
                scrollController: _scrollController_h_tableEndLine,
                onNotifyScrollChangePosition: (position) {
                  if (_currentScrollControllerIndex == 3) {
                    if (widget.dataTableColumnsLabels.isNotEmpty) {
                      _scrollController_h_tableTitleLabel.jumpTo(position);
                    }

                    if (widget.listData.isNotEmpty) {
                      _scrollController_h_tableContext.jumpTo(position);
                    }
                  }
                },
                child: tableFrozenEndLineWidget),
            onPointerDown: (PointerDownEvent event) {
              _currentScrollControllerIndex = 3;
            })));
      } else {
        allWidgets.add(XWidgetUtils.getLayoutContainer(
            backgroundColor: tableFrozenEndLinesBackgroundColor,
            decoration: frozenDividerColor!=null?
            XWidgetUtils.getBoxDecoration(
                backgroundColor:tableFrozenEndLinesBackgroundColor,
                border: Border(top: BorderSide(color: frozenDividerColor!,width: 0.5)
                )
            ):null,
            child:tableFrozenEndLineWidget));
      }
    }
  }

  //处理有冻结列的情况
  void _handleHasFrozenColumns(List<Widget> allWidgets) {
    if (widget.dataTableColumnsLabels.isEmpty) {
      return;
    }

    //左冻结
    Widget? leftFrozenTableTileLabel;
    Widget? leftFrozenTableContext;
    Widget? leftFrozenTableEndLines;
    if (frozenLeftColumnsCount != 0) {
      //每列宽度
      Map<int, TableColumnWidth>? leftFrozenColumnWidths = _getColumnWidths(
          widget.dataTableColumnsLabels.sublist(0, frozenLeftColumnsCount));

      //表头标签
      leftFrozenTableTileLabel = Table(
        border: frozenDividerColor==null?null:TableBorder(right:BorderSide(color:frozenDividerColor!,width: 0.5,)),
        columnWidths: leftFrozenColumnWidths,
        children: [
          _getTableRow_tableTitleLabel(
              widget.dataTableColumnsLabels.sublist(0, frozenLeftColumnsCount))
        ],
      );

      //内容
      if (widget.listData.length != 0) {
        List<dynamic> leftTableContextList =
        widget.listData.map((dynamic item) {
          List<dynamic> subList = item.sublist(0, frozenLeftColumnsCount);
          return subList;
        }).toList();

        leftFrozenTableContext = Table(
          border: frozenDividerColor==null?null:TableBorder(right:BorderSide(color:frozenDividerColor!,width: 0.5,)),
          columnWidths: leftFrozenColumnWidths,
          children: leftTableContextList.map((dynamic rowItemList) {

            List<Widget> srcL=getTableItemRowWidgets(rowItemList);
            List<Widget>? newL;

            if(isShowTableBorder){
              newL=[];
              int size=srcL.length;
              for(int i=0;i<size;i++){
                if(i!=size-1){
                  newL.add(XWidgetUtils.getLayoutContainer(
                      decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                      child: srcL[i]
                  ));
                }else{
                  newL.add(srcL[i]);
                }
              }
            }

            return TableRow(
              children: newL??srcL,
              decoration: XWidgetUtils.getBoxDecoration(
                  border: Border(
                      bottom: BorderSide(color: tableBorderColor, width: 0.5))),
            );

            //return getTableRow(rowItemList);
          }).toList(),
        );
      }

      //冻结最后n行数据(统计)
      if (widget.listData2.length != 0) {
        //
        List<dynamic> leftFrozenTableEndLinesList =
        widget.listData2.map((dynamic item) {
          List<dynamic> subList = item.sublist(0, frozenLeftColumnsCount);
          return subList;
        }).toList();

        leftFrozenTableEndLines = Table(
          border: frozenDividerColor==null?null:TableBorder(right:BorderSide(color:frozenDividerColor!,width: 0.5,)),
          columnWidths: leftFrozenColumnWidths,
          children: leftFrozenTableEndLinesList.map((dynamic rowItemList) {

            List<Widget> srcL=getTableEndFrozenRowWidgets(rowItemList)!;
            List<Widget>? newL;

            if(isShowTableBorder){
              newL=[];
              int size=srcL.length;
              for(int i=0;i<size;i++){
                if(i!=size-1){
                  newL.add(XWidgetUtils.getLayoutContainer(
                      decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                      child: srcL[i]
                  ));
                }else{
                  newL.add(srcL[i]);
                }
              }
            }

            return TableRow(children: newL??srcL);
          }).toList(),
        );
      }
    }

    //右冻结
    Widget? rightFrozenTableTileLabel;
    Widget? rightFrozenTableContext;
    Widget? rightFrozenTableEndLines;
    if (frozenRightColumnsCount != 0) {
      //每列宽度
      Map<int, TableColumnWidth>? rightFrozenColumnWidths = _getColumnWidths(
          widget.dataTableColumnsLabels.sublist(
              widget.dataTableColumnsLabels.length - frozenRightColumnsCount,
              widget.dataTableColumnsLabels.length));

      //表头标签
      rightFrozenTableTileLabel = Table(
        border: frozenDividerColor==null?null:TableBorder(left:BorderSide(color:frozenDividerColor!,width: 0.5,)),
        columnWidths: rightFrozenColumnWidths,
        children: [
          _getTableRow_tableTitleLabel(widget.dataTableColumnsLabels.sublist(
              widget.dataTableColumnsLabels.length - frozenRightColumnsCount,
              widget.dataTableColumnsLabels.length))
        ],
      );

      //中间表内容
      if (widget.listData.length != 0) {
        List<dynamic> rightTablContextList =
        widget.listData.map((dynamic item) {
          List<dynamic> subList = item.sublist(
              widget.dataTableColumnsLabels.length - frozenRightColumnsCount,
              widget.dataTableColumnsLabels.length);
          return subList;
        }).toList();

        rightFrozenTableContext = Table(
          border: frozenDividerColor==null?null:TableBorder(left:BorderSide(color:frozenDividerColor!,width: 0.5,)),
          columnWidths: rightFrozenColumnWidths,
          children: rightTablContextList.map((dynamic rowItemList) {

            List<Widget> srcL=getTableItemRowWidgets(rowItemList);
            List<Widget>? newL;

            if(isShowTableBorder){
              newL=[];
              int size=srcL.length;
              for(int i=0;i<size;i++){
                if(i!=size-1){
                  newL.add(XWidgetUtils.getLayoutContainer(
                      decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                      child: srcL[i]
                  ));
                }else{
                  newL.add(srcL[i]);
                }
              }
            }


            return TableRow(
              children: newL??srcL,
              decoration: XWidgetUtils.getBoxDecoration(
                  border: Border(
                      bottom: BorderSide(color: tableBorderColor, width: 0.5))),
            );

            //return getTableRow(rowItemList);
          }).toList(),
        );
      }

      //冻结最后n行数据(统计)
      if (widget.listData2.length != 0) {
        //
        List<dynamic> rightFrozenTableEndLinesList =
        widget.listData2.map((dynamic item) {
          List<dynamic> subList = item.sublist(
              widget.dataTableColumnsLabels.length - frozenRightColumnsCount,
              widget.dataTableColumnsLabels.length);
          return subList;
        }).toList();

        rightFrozenTableEndLines = Table(
          border: frozenDividerColor==null?null:TableBorder(left:BorderSide(color:frozenDividerColor!,width: 0.5,)),
          columnWidths: rightFrozenColumnWidths,
          children: rightFrozenTableEndLinesList.map((dynamic rowItemList) {

            List<Widget> srcL=getTableEndFrozenRowWidgets(rowItemList)!;
            List<Widget>? newL;

            if(isShowTableBorder){
              newL=[];
              int size=srcL.length;
              for(int i=0;i<size;i++){
                if(i!=size-1){
                  newL.add(XWidgetUtils.getLayoutContainer(
                      decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                      child: srcL[i]
                  ));
                }else{
                  newL.add(srcL[i]);
                }
              }
            }

            return TableRow(children: newL??srcL);
          }).toList(),
        );
      }
    }

    //中间
    //每列宽度
    Map<int, TableColumnWidth>? centerColumnWidths = _getColumnWidths(
        widget.dataTableColumnsLabels.sublist(frozenLeftColumnsCount,
            widget.dataTableColumnsLabels.length - frozenRightColumnsCount));
    Widget centerTableTileLabel = Table(
      //border: tableBorder,
      columnWidths: centerColumnWidths,
      children: [
        _getTableRow_tableTitleLabel(widget.dataTableColumnsLabels.sublist(
            frozenLeftColumnsCount,
            widget.dataTableColumnsLabels.length - frozenRightColumnsCount))
      ],
    );

    Widget? centerTableContextWidget;
    if (widget.listData.length != 0) {
      List<dynamic> centerTableContexList = widget.listData.map((dynamic item) {
        List<dynamic> subList = item.sublist(frozenLeftColumnsCount,
            widget.dataTableColumnsLabels.length - frozenRightColumnsCount);
        return subList;
      }).toList();

      centerTableContextWidget = Table(
        //border: tableBorder,
        columnWidths: centerColumnWidths,
        children: centerTableContexList.map((dynamic rowItemList) {

          List<Widget> srcL=getTableItemRowWidgets(rowItemList);
          List<Widget>? newL;

          if(isShowTableBorder){
            newL=[];
            int size=srcL.length;
            for(int i=0;i<size;i++){
              if(i!=size-1){
                newL.add(XWidgetUtils.getLayoutContainer(
                    decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                    child: srcL[i]
                ));
              }else{
                newL.add(srcL[i]);
              }
            }
          }

          return TableRow(
            children: newL??srcL,
            decoration: XWidgetUtils.getBoxDecoration(
                border: Border(
                    bottom: BorderSide(color: tableBorderColor, width: 0.5))),
          );

          //return getTableRow(rowItemList);
        }).toList(),
      );
    }

    //冻结最后n行数据(统计)
    Widget? centerTableEndLinesWidget;
    if (widget.listData2.length != 0) {
      //
      List<dynamic> centerTableEndLinesList =
      widget.listData2.map((dynamic item) {
        List<dynamic> subList = item.sublist(frozenLeftColumnsCount,
            widget.dataTableColumnsLabels.length - frozenRightColumnsCount);
        return subList;
      }).toList();

      centerTableEndLinesWidget = Table(
        //border: tableBorder,
        columnWidths: centerColumnWidths,
        children: centerTableEndLinesList.map((dynamic rowItemList) {

          List<Widget> srcL=getTableEndFrozenRowWidgets(rowItemList)!;
          List<Widget>? newL;

          if(isShowTableBorder){
            newL=[];
            int size=srcL.length;
            for(int i=0;i<size;i++){
              if(i!=size-1){
                newL.add(XWidgetUtils.getLayoutContainer(
                    decoration: XWidgetUtils.getBoxDecoration(border: Border(right: BorderSide(color: tableBorderColor,width: 0.5))),
                    child: srcL[i]
                ));
              }else{
                newL.add(srcL[i]);
              }
            }
          }

          return TableRow(children: newL??srcL);
        }).toList(),
      );
    }

    //表头UI
    if (_isOverScreenWidth) {
      allWidgets.add(
          XWidgetUtils.getLayoutContainer(
              backgroundColor: tableTitleLabelRowBackgroundColor,
              decoration: tableTitleLabelBottomDividerColor!=null?
              XWidgetUtils.getBoxDecoration(
                  backgroundColor:tableTitleLabelRowBackgroundColor,
                  border: Border(bottom: BorderSide(color: tableTitleLabelBottomDividerColor!,width: 0.5)
                  )
              ):null,
              child:XWidgetUtils.getLayoutRow(children: [
                leftFrozenTableTileLabel ?? XWidgetUtils.getLayoutContainer(),
                XWidgetUtils.getLayoutExpanded(XWidgetUtils.getWidgetListener(
                    child: XWidgetUtils.getLayoutSingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        //initialScrollOffset: initialHorizontalScrollOffset,
                        scrollController: _scrollController_h_tableTitleLabel,
                        onNotifyScrollChangePosition: (position) {
                          if (_currentScrollControllerIndex == 1) {
                            if (widget.listData.isNotEmpty) {
                              _scrollController_h_tableContext.jumpTo(position);
                            }

                            if (widget.listData2.isNotEmpty) {
                              _scrollController_h_tableEndLine.jumpTo(position);
                            }
                          }
                        },
                        child: centerTableTileLabel),
                    onPointerDown: (PointerDownEvent event) {
                      _currentScrollControllerIndex = 1;
                    })),
                rightFrozenTableTileLabel ?? XWidgetUtils.getLayoutContainer(),
              ])
          )
      );
    } else {
      allWidgets.add(
          XWidgetUtils.getLayoutContainer(
              backgroundColor: tableTitleLabelRowBackgroundColor,
              decoration: tableTitleLabelBottomDividerColor!=null?
              XWidgetUtils.getBoxDecoration(
                  backgroundColor:tableTitleLabelRowBackgroundColor,
                  border: Border(bottom: BorderSide(color: tableTitleLabelBottomDividerColor!,width: 0.5)
                  )
              ):null,
              child:XWidgetUtils.getLayoutRow(children: [
                leftFrozenTableTileLabel ?? XWidgetUtils.getLayoutContainer(),
                XWidgetUtils.getLayoutExpanded(centerTableTileLabel),
                rightFrozenTableTileLabel ?? XWidgetUtils.getLayoutContainer(),
              ]
              )
          )
      );
    }

    //内容
    allWidgets.add(XWidgetUtils.getLayoutExpanded(
        XWidgetUtils.getLayoutSingleChildScrollView(
          //支持分页
            initialScrollOffset: widget.currentListViewPosition,
            scrollController: xScrollController,
            onScrollNotification: onScrollNotification,
            onRefresh: !getIsEnableRefreshLoadData()?null:() async{
              widget.currentListViewPosition=0.0;
              updateData(pageIndex: getIsPageIndexStartWith1()?1:0);
            },
            child: _isOverScreenWidth
                ? XWidgetUtils.getLayoutRow(children: [
              leftFrozenTableContext ?? XWidgetUtils.getLayoutContainer(),
              XWidgetUtils.getLayoutExpanded(
                  XWidgetUtils.getWidgetListener(
                      child: XWidgetUtils.getLayoutSingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          scrollController:
                          _scrollController_h_tableContext,
                          //initialScrollOffset: initialHorizontalScrollOffset,
                          onNotifyScrollChangePosition: (position) {
                            if (_currentScrollControllerIndex == 2) {
                              if (widget
                                  .dataTableColumnsLabels.isNotEmpty) {
                                _scrollController_h_tableTitleLabel
                                    .jumpTo(position);
                              }
                              if (widget.listData2.isNotEmpty) {
                                _scrollController_h_tableEndLine
                                    .jumpTo(position);
                              }
                            }
                          },
                          child: centerTableContextWidget ??
                              XWidgetUtils.getLayoutContainer()),
                      onPointerDown: (PointerDownEvent event) {
                        _currentScrollControllerIndex = 2;
                      })),
              rightFrozenTableContext ??
                  XWidgetUtils.getLayoutContainer(),
            ])
                : XWidgetUtils.getLayoutRow(children: [
              leftFrozenTableContext ?? XWidgetUtils.getLayoutContainer(),
              XWidgetUtils.getLayoutExpanded(centerTableContextWidget ??
                  XWidgetUtils.getLayoutContainer()),
              rightFrozenTableContext ??
                  XWidgetUtils.getLayoutContainer(),
            ]))
    )
    );

    //冻结表尾UI
    if (_isOverScreenWidth) {
      allWidgets.add(
          XWidgetUtils.getLayoutContainer(
              backgroundColor: tableFrozenEndLinesBackgroundColor,
              decoration: frozenDividerColor!=null?
              XWidgetUtils.getBoxDecoration(
                  backgroundColor:tableFrozenEndLinesBackgroundColor,
                  border: Border(top: BorderSide(color: frozenDividerColor!,width: 0.5)
                  )
              ):null,
              child:XWidgetUtils.getLayoutRow(children: [
                leftFrozenTableEndLines ?? XWidgetUtils.getLayoutContainer(),
                XWidgetUtils.getLayoutExpanded(XWidgetUtils.getWidgetListener(
                    child: XWidgetUtils.getLayoutSingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        //initialScrollOffset: initialHorizontalScrollOffset,
                        scrollController: _scrollController_h_tableEndLine,
                        onNotifyScrollChangePosition: (position) {
                          if (_currentScrollControllerIndex == 3) {
                            if (widget.dataTableColumnsLabels.isNotEmpty) {
                              _scrollController_h_tableTitleLabel.jumpTo(position);
                            }

                            if (widget.listData.isNotEmpty) {
                              _scrollController_h_tableContext.jumpTo(position);
                            }
                          }
                        },
                        child: centerTableEndLinesWidget ??
                            XWidgetUtils.getLayoutContainer()),
                    onPointerDown: (PointerDownEvent event) {
                      _currentScrollControllerIndex = 3;
                    })),
                rightFrozenTableEndLines ?? XWidgetUtils.getLayoutContainer(),
              ])));
    } else {
      allWidgets.add(XWidgetUtils.getLayoutContainer(
          backgroundColor: tableFrozenEndLinesBackgroundColor,
          decoration: frozenDividerColor!=null?
          XWidgetUtils.getBoxDecoration(
              backgroundColor:tableFrozenEndLinesBackgroundColor,
              border: Border(top: BorderSide(color: frozenDividerColor!,width: 0.5)
              )
          ):null,
          child:XWidgetUtils.getLayoutRow(children: [
            leftFrozenTableTileLabel ?? XWidgetUtils.getLayoutContainer(),
            XWidgetUtils.getLayoutExpanded(centerTableTileLabel),
            rightFrozenTableTileLabel ?? XWidgetUtils.getLayoutContainer(),
          ])));
    }
  }


  //增删item公开方法 分页///////////////////////////////////////////////////////////////
  void setXListData_pageIndexData(int? pageIndex, int? pageCount, List<dynamic>? items){
    if(mounted){
      setState((){

        pageIndex??=0;
        pageCount??=0;

        //判断是否删除所有数据
        if(getIsPageIndexStartWith1()){
          //分页从第1页开始
          if(pageIndex==0 || pageIndex==1){
            widget.listData.clear();
          }
        }else{
          //分页从0开始
          if(pageIndex==0){
            widget.listData.clear();
          }
        }


        //从第二页开始,判断是否重复加载,防止抖动多次触发加载
        if((getIsPageIndexStartWith1() && pageIndex!>1) || (!getIsPageIndexStartWith1() && pageIndex!>0)){
          if(pageIndex==widget.currentPageIndex){
            return;
          }
        }

        widget.currentPageIndex=pageIndex!;
        widget.totalPages=pageCount!;

        //
        if(items!=null){
          widget.listData.addAll(items);
        }

      });
    }

  }

  //追加多条
  void setXListData_addAllItems(
      List<dynamic>? items,
      {
        int? position, //插入位置
        bool isReplaceAllItems=true,//替换所有数据
      }
      ){

    if(mounted){
      //
      if(isReplaceAllItems){
        //删除所有数据
        widget.listData.clear();
      }

      if(items!=null){
        //先添加item,后更新
        if(position==null || position<0){
          //在末尾添加
          position=widget.listData.length;
          widget.listData.addAll(items);
        }else{
          //按位置插入
          widget.listData.insertAll(position, items);
        }
      }

      setState((){
      });
    }

  }




  /*
    设置每列宽度
    IntrinsicColumnWidth item的实际宽度
    FixedColumnWidth  自定义固定宽度
    */
  Map<int, TableColumnWidth> _getColumnWidths(
      List<XBeanDataTableLabel> tableColumnsLabels) {
    Map<int, TableColumnWidth> columnWidths = {};
    for (int i = 0; i < tableColumnsLabels.length; i++) {
      XBeanDataTableLabel citem = tableColumnsLabels[i];
      if (citem.columnWidth != null) {
        columnWidths[i] = FixedColumnWidth(citem.columnWidth!);
      } else {
        if (_isOverScreenWidth) {
          //超出屏幕宽度,默认60
          columnWidths[i] = const FixedColumnWidth(60);
        } else {
          //未超出屏幕宽度,按弹性比例
          columnWidths[i] = FlexColumnWidth(citem.flex);
        }
      }
    }
    return columnWidths;
  }

  //开始防抖计时
  void _startDebounceTimer() {
    _cancelDebounceTimer(); // 取消之前的计时器
    _scrollEndTimer = Timer(const Duration(milliseconds: 300), () {
      // 检查期间是否有新滚动
      if (!_isScrolling) {
        //加载下一页
        updateData(pageIndex: widget.currentPageIndex+1);
      }
    });
  }

  //关闭防抖计时
  void _cancelDebounceTimer() {
    _scrollEndTimer?.cancel();
    _scrollEndTimer = null;
  }

  //表头标题TableRow
  TableRow _getTableRow_tableTitleLabel(
      List<XBeanDataTableLabel> tableColumnsLabels) {
    return TableRow(
      children: tableColumnsLabels.map((XBeanDataTableLabel label) {
        TextAlign textAlign;
        if (label.alignment == Alignment.centerLeft ||
            label.alignment == Alignment.topLeft ||
            label.alignment == Alignment.bottomLeft) {
          textAlign = TextAlign.start;
        } else if (label.alignment == Alignment.centerRight ||
            label.alignment == Alignment.topRight ||
            label.alignment == Alignment.bottomRight) {
          textAlign = TextAlign.end;
        } else {
          textAlign = TextAlign.center;
        }

        bool isLastItem= label==tableColumnsLabels.last;


        Widget columnItemWidget = XWidgetUtils.getLayoutContainer(
            width: label.columnWidth,
            height: tableTitleLabelRowHeight,
            backgroundColor: label.backgroundColor,
            decoration: (isShowTableBorder && !isLastItem)?XWidgetUtils.getBoxDecoration(
              border: Border(right: BorderSide(color: tableBorderColor,width: 0.5)),
            ):null,
            child: XWidgetUtils.getWidgetText(
              label.text,
              textAlign: textAlign,
              style: label.style,
            ),
            alignment: label.alignment);

        return columnItemWidget;
      }).toList(),
    );
  }

  //防抖处理(适配滑动到底部触发)
  Timer? _scrollEndTimer;
  bool _isScrolling = false;

}
